/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.altuure.yagdao.spring.hibernate;

import com.altuure.yagdao.hibernate.spring.SpringGenericHibernateDAOFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import java.util.Map;
import java.util.Set;

public class HibernateConfigDefinitionParser implements BeanDefinitionParser {

    private static final Log log = LogFactory.getLog(HibernateConfigDefinitionParser.class);

    public BeanDefinition parse(final Element element, final ParserContext parserContext) {
        log.debug("Parsing hibernate element");
        HibernateConfigContext configContext = new HibernateConfigContext(element, parserContext);
        configContext.validate();

        registerBeanDefinitions(configContext, parserContext);

        return null;
    }

    private void registerBeanDefinitions(HibernateConfigContext configContext, ParserContext parserContext) {

        Map<String, String> daoMap = configContext.getDaoMap();
        Set<String> keySet = daoMap.keySet();
        Object beanSource = parserContext.extractSource(configContext.getElement());
        for (String beanId : keySet) {

            BeanDefinitionBuilder builder = BeanDefinitionBuilder
                    .rootBeanDefinition(SpringGenericHibernateDAOFactory.class);

            builder.addPropertyValue("sessionAccessor", createSessionFactoryAccessor(configContext, beanSource));
            builder.addPropertyValue("targetInterfaces", daoMap.get(beanId));
            builder.setInitMethodName("initDAO");
            // builder.setFactoryMethod("getObject");

            builder.setScope(ConfigurableBeanFactory.SCOPE_SINGLETON);
            AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
            beanDefinition.setSource(beanSource);

            BeanComponentDefinition definition = new BeanComponentDefinition(beanDefinition, beanId);
            parserContext.registerBeanComponent(definition);
        }

    }

    private BeanDefinition createSessionFactoryAccessor(HibernateConfigContext context, Object beanSource) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder
                .rootBeanDefinition(com.altuure.yagdao.hibernate.spring.SpringHibernateSessionAccessor.class);

        if (context.getSessionFactory() != null)
            builder.addConstructorArgReference(context.getSessionFactory());

        org.springframework.beans.factory.support.AbstractBeanDefinition bean = builder.getRawBeanDefinition();
        bean.setSource(beanSource);
        return bean;
    }
}
